package com.btks.mapreduce.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 编写Driver驱动类
 * @author cheng-qiang
 * @date 2022年10月31日11:46
 */
public class WordCountDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 获取配置信息以及获取Job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        // 关联本Driver程序的jar
        job.setJarByClass(WordCountDriver.class);
        // 关联Mapper 和 Reducer的jar
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        // 设置Mapper 输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setCombinerClass(WordCountReducer.class);
        // 设置输入和输出目录路径
        // window环境中测试
        FileInputFormat.setInputPaths(job,new Path("E:\\hadoop\\input\\word"));
        FileOutputFormat.setOutputPath(job,new Path("E:\\hadoop\\output\\wordcount"));
        // Linux 集群环境中测试动态参数
        // FileInputFormat.setInputPaths(job,new Path(args[0]));
        // FileOutputFormat.setOutputPath(job,new Path(args[1]));
        // 如果不设置InputFormat，它默认用的是TextInputFormat.class
        job.setInputFormatClass(CombineTextInputFormat.class);
        //虚拟存储切片最大值设置20m
        CombineTextInputFormat.setMaxInputSplitSize(job, 20971520);
        // 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
